/*
 * Copyright (c)  2015~2020, xforceplus
 * All rights reserved.
 * Project:datacare-orm
 * Id: MappingFunctionHelper.java   2020-10-10 22-17-52
 * Author: Evan
 */
package cn.datacare.orm.mapping;

import cn.datacare.orm.mapping.lazy.LazyMappingEntity;
import cn.datacare.orm.mapping.orm.MappingEntity;
import cn.datacare.orm.mapping.orm.OrmSqlResultListener;
import cn.datacare.orm.source.SQLResultListener;
import org.beetl.core.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static cn.datacare.orm.constant.Constants.*;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: 2015~2020
 * </p>
 * <p>
 * Company/Department: xforceplus
 * </p>
 *
 * @author Evan
 * <b>Creation Time:</b> 2020-10-10 22-17-52
 * @since V1.0
 */
public class MappingFunctionHelper {

    public static void addMapping(Context ctx, MappingEntity mappingConfig) {
        List<SQLResultListener> ls = (List<SQLResultListener>) ctx.getGlobal(PARAM_LISTENER);
        if (ls == null) {
            ls = new ArrayList<>();
            OrmSqlResultListener orm = new OrmSqlResultListener();
            orm.getMappingEntities().add(mappingConfig);
            ls.add(orm);
            ctx.set(PARAM_LISTENER, ls);
            return;
        } else {
            for (SQLResultListener l : ls) {
                if (l instanceof OrmSqlResultListener) {
                    ((OrmSqlResultListener) l).getMappingEntities().add(mappingConfig);
                    break;
                }
            }
        }
    }


    protected void parse(boolean single, boolean lazy, Object[] paras, Context ctx) {
        if (ctx.getGlobal(_PAGE) != null) {
            //翻页求总数查询，忽略orm
            return;
        }

        String className = null;
        String sqlId = null;
        String tailName = null;
        Map<String, Object> queryParas = null;

        Object last = paras[paras.length - 1];
        int len = paras.length;
        if (last instanceof Map) {
            queryParas = (Map<String, Object>) last;
            tailName = (String) queryParas.get(ALIAS);
            queryParas.remove(ALIAS);
            if (queryParas.size() == 0) {
                queryParas = null;
            }
            //TODO fitler&Order
            len = len - 1;

        }


        if (len >= 3) {
            //使用了sqlid
            className = (String) paras[2];
            sqlId = (String) paras[1];
        } else {
            className = (String) paras[1];

        }


        MappingEntity mappingEntity = null;
        if (lazy) {
            mappingEntity = new LazyMappingEntity();
        } else {
            mappingEntity = new MappingEntity();
        }
        Map<String, String> mapKey = (Map<String, String>) paras[0];
        mappingEntity.setSingle(single);
        mappingEntity.setMapKey(mapKey);
        mappingEntity.setTarget(className);
        mappingEntity.setSqlId(sqlId);
        mappingEntity.setTailName(tailName);
        mappingEntity.setQueryParams(queryParas);

        addMapping(ctx, mappingEntity);
    }
}
